<?php
// include_once("../tools/DBInterface.class.php");

/**
* This is a representation of one delegation in DB
* @author Juan G. Bernal <juancho2986@gmail.com>
* @version 1.0
* @package Delegation
*/
class Delegation
{
    private $dbinterface;

    /**
    * Constructor function
    */
    function __construct()
    {
        $this->dbinterface = new DBInterface();
    }

    /**
    * Insert a new delegation in th DB
    * @param Array $data An associative array with the delegation data
    * @return Bool Indicates if the insertion was succesful
    */
    function create($data)
    {
        return $this->dbinterface->exec_insert($data,'delegation');
    }

    /**
    * Modify a single field of one delegation
    * @param String $fld_name Field name
    * @param String $fld_value Field value
    * @param Integer $id Id of the delegation to be modified
    */
    function modify($fld_name,$fld_value,$id)
    {
        $data = array();
        $data[$fld_name] = $fld_value;
        return $this->dbinterface->exec_update($data,'delegation','del_id='.$id);
    }

    /**
    * Delete a delegation
    * @param Integer $id Id of the delegation to be deleted
    */
    function remove($id)
    {
        $this->dbinterface->exec_delete('delegation','del_id='.$id);
    }

    /**
    * Retrieve the information of one delegation
    * @param Integer $id Id of the delegation
    * @return Array Array with the delegation data
    */
    function getData($id)
    {
        $result_set = $this->dbinterface->exec_select(array('*'),'delegation','del_id='.$id);
        return $result_set[0];
    }

    /**
    * Retrieve a list of delegations accord of search criteria
    * @param String $name A part or complete name of one delegation, this is optional
    * @return Array Bidimensional array with the delegations data
    */
    function getList($name='')
    {
        $name!='' ? $criteria = "del_name ilike '%".$name."%' and del_id<>1" : $criteria = 'del_id<>1';
        return $this->dbinterface->exec_select(array('*'),'delegation',$criteria);
    }

    /**
    * Validate if exist a delegation with the same input name
    * @param String $name Input name
    * @return Bool Indicates if exists a delegation
    */
    function exist($name)
    {
        $delegations = $this->dbinterface->exec_select(array('del_id'),'delegation',"del_name='".$name."'");

        if($delegations==0)
            return false;
        else
            return true;
    }

    /**
    * Validate if the quota of the delegation is full
    * @param Integer $id Id of the delegation
    * @param String $gender Gender for the quota
    * @return Bool Indicates if the quota its full
    */
    function isFull($id,$gender)
    {
        $gender=='m' ? $fields = array('del_sportsman_number as quota') : $fields = array('del_sportswoman_number as quota');
        $quota = $this->dbinterface->exec_select($fields,'delegation','del_id='.$id);

        if($quota==0)
            return true;

        if($quota[0]['quota']==0)
            return true;

        $fields = array('count(pla_id) as num','pla_del_id');
        $criteria = "pla_del_id=del_id and pla_gender='".$gender."' and pla_del_id=".$id;
        $other = "group by pla_del_id";
        $tables = array('player','delegation');
        $number = $this->dbinterface->exec_select($fields,$tables,$criteria,$other);

        if($quota[0]['quota']==$number[0]['num'])
            return true;
        else
            return false;
    }

    /**
    * Obtains a name list of delegations
    * @param String $name A part or complete name of one delegation
    * @return Array The names array
    */
    function getNames($name)
    {
        $criteria = "del_name ilike '%".$name."%'";
        $names = $this->dbinterface->exec_select(array('del_name as name'),'delegation',$criteria);
        return $names;
    }

    /**
    * Obtains the number of players registered in one delegation
    * @param Integer $id Id of the delegation
    * @param String $gender Gender for the quota
    * @return Integer The number of players
    */
    function getUsedQuota($id,$gender)
    {
        $criteria = "pla_del_id=".$id." and pla_gender='".$gender."'";
        $group = "group by pla_gender";
        $numbers = $this->dbinterface->exec_select(array('count(pla_id) as number','pla_gender'),'player',$criteria,$group);
        return $numbers[0]['number'];
    }

    /**
    * Verify if a delegation has games associated
    * @param Integer $id Id of the delegation
    * @return Bool Indicates if the delegation has games associated
    */
    function hasGames($id)
    {
        $criteria = "gme_del_id_1=".$id." or gme_del_id_2=".$id;
        $games = $this->dbinterface->exec_select(array('*'),'game',$criteria);
        if($games==0)
            return false;
        else
            return true;
    }
}
?>